<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018\4\17 0017
 * Time: 11:59
 */
namespace app\index\model;
class Upfiles
{
    static function Send($url,$file,$type,$size=2000000){
        foreach($file as $keys=>$value){
            if(is_array($value['name'])){
//                return 1;
                foreach($value['name'] as $key=>$val){
                    $values[$key]['name']=$value['name'][$key];
                    $values[$key]['type']=$value['type'][$key];
                    $values[$key]['tmp_name']=$value['tmp_name'][$key];
                    $values[$key]['error']=$value['error'][$key];
                    $values[$key]['size']=$value['size'][$key];
                }
//                return $values;
                foreach($values as $b){
                    if($b['error']){return 0;}
                    if($b['size']>$size){return 0;}
                    $str=explode('/',$b['type'])[1];
                    if($type&&count($type)){
                        if(!in_array($str,$type)){return 0;}
                    }
                    $name=uniqid().mt_rand(0,9000);
//                    return $url.'/'.$name.'.'.$str;
                    $mio=move_uploaded_file($b['tmp_name'],$url.'/'.$name.'.'.$str);
//                    return $mio;
                    if($mio){$data[]=$name.'.'.$str;}
                }
            }else{

                if($value['name']){
                    if($value['error']){return 0;}
                    if($value['size']>$size){return 0;}
                    $str=explode('/',$value['type'])[1];
                    //return var_dump(in_array($str,$type));
                    if($type&&count($type)){
                        if(!in_array($str,$type)){return 0;}
                    }
                    $name=uniqid().mt_rand(0,9000);
                    $mio=move_uploaded_file($value['tmp_name'],$url.'/'.$name.'.'.$str);
                    if($mio){
                        $data[]=$name.'.'.$str;
                    }
                } else{
                    $data= null;
                }
            }
        }
        return $data;
    }
    /**保存图片，保存以name[]传过来的文件，带名称
     * @param $url：保存地址
     * @param $file 文件
     * @param $type 允许的类型组成的数组
     * @param int $size：最大允许多大
     * @return array|null
     */
    static function Sends($url,$file,array $type,$size=2000000){
        foreach($file as $keys=>$value){
            if(is_array($value['name'])){
                foreach($value['name'] as $key=>$val){
                    $values[$key]['name']=$value['name'][$key];
                    $values[$key]['type']=$value['type'][$key];
                    $values[$key]['tmp_name']=$value['tmp_name'][$key];
                    $values[$key]['error']=$value['error'][$key];
                    $values[$key]['size']=$value['size'][$key];
                }
                foreach($values as $b){
                    if($b['error']){return 0;}
                    if($b['size']>$size){return 0;}
                    $str=explode('/',$b['type'])[1];
                    if(!in_array($str,$type)){return 0;}
                    $name=uniqid().mt_rand(0,9000);
                    $mio=move_uploaded_file($b['tmp_name'],$url.'/'.$name.'.'.$str);
                    if($mio){$data[$keys][]=$name.'.'.$str;}
                }
            }else{
                if($value['name']){
                    if($value['error']){return 0;}
                    if($value['size']>$size){return 0;}
                    $str=explode('/',$value['type'])[1];
                    if(!in_array($str,$type)){return 0;}
                    $name=uniqid().mt_rand(0,9000);
                    $mio=move_uploaded_file($value['tmp_name'],$url.'/'.$name.'.'.$str);
                    if($mio)$data[$keys]=$name.'.'.$str;
                }else{
                    $data [$keys]= null;
                }
            }
        }
        return $data;
    }
    /**保存图片，保存以name传过来的文件，单张
     * @param $url：保存地址
     * @param $file 文件
     * @param $type 允许的类型组成的数组
     * @param int $size：最大允许多大
     * @return array|null
     */
    static function Getone($url,$array,array $type=['png','jpeg','gif'],$size=2000000){
        if($array['error']){
            return 0;
        }
//        return 1;
        if($array['size']>$size){
            return 0;
        }
//        return 1;
        $str=explode('/',$array['type'])[1];
        if(!in_array($str,$type)){
            return 0;
        }
//        return 1;
        $name=uniqid().mt_rand(0,9000);
//        return $array['tmp_name'];
        $mio=move_uploaded_file($array['tmp_name'],$url.'/'.$name.'.'.$str);
//        return 1;
        if($mio){
            return $name.'.'.$str;
        }else{
            return 0;
        }
    }
}